"""
Case Type   : SQL_LIMIT
Case Name   : 主备集群环境创建规则时，work_node设置为-1，创建规则失败
Create At   : 2025/9/16
Owner       : lonely-dance
Description :
    1、修改参数;
    2、创建规则，work_node设置为-1
    3、清理环境
Expect      :
    1、修改成功
    2、创建失败
    3、清理环境成功
History     :
"""

import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class SqlLimit(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----- {os.path.basename(__file__)} start-----')
        self.comm_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.Constant = Constant()
        self.limit_name1 = 'limit0032_01'
        self.table_name = 'sql_limit0032'
        self.limit_id1 = 0

    def test_main(self):
        text = '----step1:修改参数enable_sql_limit=on expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = on')
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'instr_unique_sql_count = 1000')

        text = '----step2:创建规则，work_node设置为-1 expect:成功----'
        self.log.info(text)
        cmd = f'''
        select gs_create_sql_limit('{self.limit_name1}','select',-1,0,now(),'2035-3-30 23:59:59','{{{{select,from}}}}','','');
        '''
        self.log.info(cmd)
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn('ERROR:  tinyint out of range', res, '创建限流limit失败')

    def tearDown(self):
        text = '----step3:恢复环境 expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = off')
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'instr_unique_sql_count = 100')
        self.log.info(f'----- {os.path.basename(__file__)} end-----')
